<template>
	<div class="my_l">
		<header class="top_bar">
			<a onclick="window.history.go(-1)" class="icon_back"></a>
			<h3 class="cartname">用户登录</h3>
		</header>
		<main class="user_login_box">
			<div class="login_dialog">
				<div class="_username">
					<input type="text" name="username" placeholder="请输入用户名" class="user_input" v-model="username" />
				</div>
				<div class="_username u_passwd">
					<input type="password" name="password" placeholder="请输入密码" class="user_input" v-model="password" />
				</div>

				<div class="login_box">
					<a @click="goLogin()" class="btn_login">登录</a>
				</div>
			</div>
		</main>
	</div>
</template>

<script>
import { MessageBox } from 'mint-ui'

export default {
  components: {
    MessageBox
  },
  data() {
    return {
      username: '',
      password: '',
      userInfo: {}
    }
  },
  methods: {
    goLogin() {
      let _this = this
      let openid  = window.sessionStorage.openid
      if (_this.username == '') {
        MessageBox.alert('请输入用户名')
      } else if (_this.password == '') {
        MessageBox.alert('请输入密码')
      } else {
        _this.$http
          .get('/mechanismadmin/mobile/login', {
            params: {
              username: _this.username,
              password: _this.password,
              openid:openid
            }
          })
          .then(
            res => {
              console.log(res)
              if (res.data.errno == 0) {
                _this.userInfo = res.data.data
                //LOGIN success
                window.sessionStorage.userInfo = JSON.stringify(_this.userInfo)
                console.log(_this.$store)
                _this.$store.dispatch('setUserInfo', _this.userInfo)
                let redirect = decodeURIComponent(_this.$route.query.redirect || '/')
                _this.$router.push({
                  path: redirect
                })
              } else {
				  MessageBox('提示', res.data.errmsg)
              }
              console.log(res)
            },
            err => {
              console.log(err)
            }
          )
      }
    }
  }
}
</script>
<style>
@import '../assets/css/login.css';
</style>